<script type="text/javascript">
    RED.nodes.registerType('ui_text',{
        category: RED._("node-red-dashboard/ui_base:ui_base.label.category"),
        color: 'rgb(119, 198, 204)',
        defaults: {
            group: {type: 'ui_group', required:true},
            order: {value: 0},
            width: {value: 0, validate: function(v) {
                    var width = v||0;
                    var currentGroup = $('#node-input-group').val()||this.group;
                    var groupNode = RED.nodes.node(currentGroup);
                    var valid = !groupNode || +width <= +groupNode.width;
                    $("#node-input-size").toggleClass("input-error",!valid);
                    return valid;
                }
            },
            height: {value: 0},
            name: {value: ''},
            label: {value: 'text'},
            format: {value: '{{msg.payload}}'},
            layout: {value:'row-spread'},
            className: {value: ''}
        },
        inputs:1,
        outputs:0,
        align: "right",
        icon: "ui_text.png",
        paletteLabel: 'text',
        label: function() { return this.name || (~this.label.indexOf("{{") ? null : this.label) || 'text'; },
        labelStyle: function() { return this.name?"node_label_italic":""; },
        oneditprepare: function() {
            $("#node-input-size").elementSizer({
                width: "#node-input-width",
                height: "#node-input-height",
                group: "#node-input-group"
            });

            $(".nr-db-text-layout-"+(this.layout||'row-spread')).addClass('selected');

            [ ".nr-db-text-layout-row-left",".nr-db-text-layout-row-center",".nr-db-text-layout-row-right",
              ".nr-db-text-layout-row-spread",".nr-db-text-layout-col-center"].forEach(function(id) {
                    $(id).click(function(e) {
                        $(".nr-db-text-layout").removeClass('selected');
                        $(this).addClass('selected');
                        $('#node-input-layout').val(id.substring(".nr-db-text-layout-".length));
                        e.preventDefault();
                    })
                })
        }
    });
</script>

<script type="text/html" data-template-name="ui_text">
    <div class="form-row">
        <label for="node-input-group"><i class="fa fa-table"></i> Group</label>
        <input type="text" id="node-input-group">
    </div>
    <div class="form-row">
        <label><i class="fa fa-object-group"></i> Size</label>
        <input type="hidden" id="node-input-width">
        <input type="hidden" id="node-input-height">
        <button class="editor-button" id="node-input-size"></button>
    </div>
    <div class="form-row">
        <label for="node-input-label"><i class="fa fa-i-cursor"></i> Label</label>
        <input type="text" id="node-input-label">
    </div>
    <div class="form-row">
        <label for="node-input-format"><i class="fa fa-i-cursor"></i> Value format</label>
        <input type="text" id="node-input-format" placeholder="{{msg.payload}}">
    </div>
    <div class="form-row">
        <label style="vertical-align: top"><i class="fa fa-th-large"></i> Layout</label>
        <div style="display:inline-block">
        <input type="hidden" id="node-input-layout"><input type="hidden" id="node-input-layoutAlign">
        <div>
            <a href="#" class="nr-db-text-layout nr-db-text-layout-row nr-db-text-layout-row-left">
                <span class="nr-db-text-layout-label">label</span>
                <span class="nr-db-text-layout-value">value</span>
                <div class="nr-db-text-layout-checkbox"></div>
            </a>
            <a href="#" class="nr-db-text-layout nr-db-text-layout-row nr-db-text-layout-row-center">
                <span class="nr-db-text-layout-label">label</span>
                <span class="nr-db-text-layout-value">value</span>
                <div class="nr-db-text-layout-checkbox"></div>
            </a>
            <a href="#" class="nr-db-text-layout nr-db-text-layout-row nr-db-text-layout-row-right">
                <span class="nr-db-text-layout-label">label</span>
                <span class="nr-db-text-layout-value">value</span>
                <div class="nr-db-text-layout-checkbox"></div>
            </a>
        </div>
        <div>
            <a href="#" class="nr-db-text-layout nr-db-text-layout-row nr-db-text-layout-row-spread">
                <span class="nr-db-text-layout-label">label</span>
                <span class="nr-db-text-layout-value">value</span>
                <div class="nr-db-text-layout-checkbox"></div>
            </a>
            <a href="#" class="nr-db-text-layout nr-db-text-layout-col nr-db-text-layout-col-center">
                <span class="nr-db-text-layout-label">label</span>
                <span class="nr-db-text-layout-value">value</span>
                <div class="nr-db-text-layout-checkbox"></div>
            </a>
        </div>
        </div>
    </div>
    <div class="form-row">
        <label for="node-input-className"><i class="fa fa-code"></i>  Class</label>
        <input type="text" id="node-input-className" placeholder="Optional CSS class name(s) for widget"/>
    </div>
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name">
    </div>


</script>
<style>
    .nr-db-text-layout {
        position:relative;
        display: inline-block;
        width: 90px;
        height: 60px;
        border-radius:3px;
        border:1px solid #bbb;
        cursor:pointer;
        color: #666;
        margin-right: 10px;
        margin-bottom: 10px;
    }
    .nr-db-text-layout.selected, .nr-db-text-layout:hover {
        border-color: #333;
        color: #333;
    }
    .nr-db-text-layout span {
        position: absolute;
    }
    .nr-db-text-layout-value {
        font-weight: bold;
    }
    .nr-db-text-layout-row span { top: 20px; }
    .nr-db-text-layout-row-left .nr-db-text-layout-label { left: 2px; }
    .nr-db-text-layout-row-left .nr-db-text-layout-value { left: 34px; }
    .nr-db-text-layout-row-spread .nr-db-text-layout-label { left: 2px; }
    .nr-db-text-layout-row-spread .nr-db-text-layout-value { right: 2px; }
    .nr-db-text-layout-row-center .nr-db-text-layout-label { left: 11px; }
    .nr-db-text-layout-row-center .nr-db-text-layout-value { right: 11px; }
    .nr-db-text-layout-row-right .nr-db-text-layout-label { right: 40px; }
    .nr-db-text-layout-row-right .nr-db-text-layout-value { right: 2px; }

    .nr-db-text-layout-col span { width: 90px;  text-align: center; left: 0px;}
    .nr-db-text-layout-col-center .nr-db-text-layout-label { top: 12px; }
    .nr-db-text-layout-col-center .nr-db-text-layout-value { top: 26px; }
    .nr-db-text-layout-checkbox {
        display: none;
        width: 10px;
        height: 10px;
        border-radius: 10px;
        border: 1px solid #bbb;
        position: absolute;
        right: -5px;
        top: -5px;
        background: #fff;
    }
    .nr-db-text-layout.selected .nr-db-text-layout-checkbox {
        display:inline-block;
        box-shadow: inset 0px 0px 0px 2px #fff;
        background: #333;
        border-color: #333;
    }

</style>
